package com.hqyj.springboot_project01.test.controller;

import com.hqyj.springboot_project01.common.entity.Result;
import com.hqyj.springboot_project01.test.entity.City;
import com.hqyj.springboot_project01.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class CityController {
    @Autowired
    private CityService cityService;

    /**
     * desc
     *
     * @param city
     * @return 127.0.0.1/api/city   ---post
     * {"cityName":"dreamCity","localCityName":"梦想之城","countryId":522}
     */
    @PostMapping(value = "/city", consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city) {
        return cityService.insertCity(city);
    }

    /**
     * @param city
     * @return 127.0.0.1/api/city   ---put
     * {"cityName":"dreamCity1","localCityName":"梦想之城1","countryId":522}
     */
    @PutMapping(value = "/city", consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city) {
        return cityService.updateCity(city);
    }

    /**
     * @param cityId
     * @return 127.0.0.1/api/city/2260   ---delete
     */
    @DeleteMapping("/city/{cityId}")
    public Result<Object> deleteCityById(@PathVariable int cityId) {
        return cityService.deleteCityById(cityId);
    }

    /**
     * @param cityId
     * @return 127.0.0.1/api/city/1890   ----get
     */
    @GetMapping("/city/{cityId}")
    public City getCityById(@PathVariable int cityId) {
        return cityService.getCityById(cityId);
    }

}
